$(function () {
    $("#jqGrid").jqGrid({
        url: '../spareparttype/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '备件类型', name: 'typeName', index: 'type_name', width: 80 },
			{ label: '产品ID', name: 'proId', index: 'pro_id', width: 80 ,hidden:true },
			{ label: '语言编码', name: 'language', index: 'language', width: 80 }, 			
			{ label: '数据状态（0启用、1停用）', name: 'status', index: 'status', width: 80,hidden:true },
			{ label: '添加时间', name: 'addTime', index: 'add_time', width: 80 ,hidden:true},
			{ label: '添加人', name: 'addUser', index: 'add_user', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sparePartType: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.queryProduct();
			vm.showList = false;
			vm.title = "新增";
			vm.sparePartType = {};
		},
		queryProduct:function(){
			var url = "../product/proList";
			$.ajax({
				type: "POST",
				url: url,
				data: JSON.stringify(new Object()),
				success: function(r){
					if(r.code === 0){
						$("#proSelect").empty();
						for(var i=0;i<r.proList.length;i++)
						{
							$("#proSelect").append("<option value='"+r.proList[i].id+"'>"+r.proList[i].proName+"</option>");
						}
					}else{

					}
				}
			});
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			//赋值
			var proSelect = $("#proSelect").val();
			var language = $("#languageSelect").val();
			vm.sparePartType.language = language;
			vm.sparePartType.proId = proSelect;
			var url = vm.sparePartType.id == null ? "../spareparttype/save" : "../spareparttype/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.sparePartType),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../spareparttype/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			vm.queryProduct();
			$.get("../spareparttype/info/"+id, function(r){
                vm.sparePartType = r.sparePartType;
				$("#proSelect").find("option[text='"+vm.sparePartType.proId+"']").attr("selected",true);
				$("#languageSelect").find("option[text='"+vm.sparePartType.language+"']").attr("selected",true);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});